﻿using UnityEngine;
using System.Collections;

public class fade : MonoBehaviour {

	public Color color_from = new Color(1f,1f,1f,1f);
	public Color color_to = new Color(1f,1f,1f,0f);
	private Color color = new Color(1f,1f,1f,0f);
	public float speed = 1.0f;
	public float delay = 0.0f;
	private float t = 0.0f;
	public bool done = false;
	public bool start_by_anykey = false;
	public bool start_by_external = false;
	public bool jump_to_next_scene = false;
	
	Texture2D tex;

	// Use this for initialization
	void Start () {

		tex = new Texture2D (1,1,TextureFormat.ARGB32,false);

		color = color_from;
		tex.SetPixel(0,0,color);

		tex.Apply ();
		guiTexture.texture = tex;
	}
	
	// Update is called once per frame
	void Update () {

		if(done == false)
		{
			if(t>=delay && start_by_anykey==false && start_by_external==false)
			{
				float per = t-delay;
				color = Color.Lerp (color_from,color_to,per);
				
				t += Time.deltaTime / speed;
				
				if(per>=1f)
					done=true;
			}
			else
			{
				if(start_by_anykey)
				{
					t=0.0f;
					if(Input.anyKey)
						start_by_anykey = false;
				}
				else
					t += Time.deltaTime;
			}	
				
			//Texture2D tex = new Texture2D (1,1,TextureFormat.ARGB32,false);
			tex.SetPixel(0,0,color);
			tex.Apply ();
			guiTexture.texture = tex;
		}
		else if(jump_to_next_scene)
			Application.LoadLevel(Application.loadedLevel);
		
	
	}
}
